import time
import datetime


def format_time_one(entity,colName):
    if colName in entity:
        int_time=int(entity[colName])
        entity[f'{colName}_str']=timestamp_to_str(int_time)

def format_time(resList):
    出入时间="CRSJ_1_4_1_4"
    res=[]
    colNameList=['最后时间','FSSJ_1_11','TSTAMP_4_23_2_23_1_23_1_23','TSTAMP_1_10_1_10',出入时间,'CRSJ_1_2_1_2']
    for entity in resList:
    
        for colName in colNameList:
            format_time_one(entity,colName)
        res.append(entity)
        # pprint.pprint(entity)
        # 24 小时不在学校  如果 得到的结果里面有 周日的 去掉
    return res


ymdHeng="%Y-%m-%d"
ymdhmsHeng="%Y-%m-%d %H:%M:%S"

def strToTimestamp(date_string = "2023-07-03",date_format = "%Y-%m-%d"):


    # date_string = "2023-07-03"
    # date_format = "%Y-%m-%d"

    # 将日期字符串转换为 datetime 对象
    dt = datetime.datetime.strptime(date_string, date_format)

    # 获取时间戳（以秒为单位）
    timestamp = int(dt.timestamp())

    # print(timestamp)
    return timestamp

def get_now_time_str():
    now_time_str = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
    return now_time_str

def day_of_week(timestamp = 1625307600):
    """
    
    from top.starp.util import time_util

    # ""
    # 获取一个日期字符串的 timestamp pyyhon

    ss=time_util.strToTimestamp("2023-07-01")
    stt=time_util.day_of_week(ss)

    print(stt)
    # 6  周6 
    """
    # import datetime

    # Assuming you have a timestamp value in seconds
    # timestamp = 1625307600

    # Convert the timestamp to a datetime object
    dt = datetime.datetime.fromtimestamp(timestamp)

    # Get the day of the week (Monday = 1, Sunday = 7)
    day_of_week = dt.isoweekday()

    # print(day_of_week)
    return day_of_week

import datetime

# timestamp = 1625068931  # 假设这是一个时间戳

# # 使用 datetime.fromtimestamp() 方法将时间戳转换为 datetime 对象
# dt_object = datetime.datetime.fromtimestamp(timestamp)

# # 使用 strftime() 方法将 datetime 对象格式化为时间字符串
# time_string = dt_object.strftime('%Y-%m-%d %H:%M:%S')

# print(time_string)
# 

def is_millisecond_timestamp(timestamp):
    # 判断位数
    if len(str(timestamp)) == 13:
        return True
    else:
        return False

def convert_to_seconds(timestamp):
    if is_millisecond_timestamp(timestamp):
        timestamp = timestamp // 1000
    return timestamp


def timestamp_to_str(timestamp,format_str='%Y-%m-%d %H:%M:%S'):
    # timestamp = 1625068931  # 假设这是一个时间戳
    # 1695398399999
    # 使用 datetime.fromtimestamp() 方法将时间戳转换为 datetime 对象
    timestamp=convert_to_seconds(timestamp)
    dt_object = datetime.datetime.fromtimestamp(timestamp)

    # 使用 strftime() 方法将 datetime 对象格式化为时间字符串
    time_string = dt_object.strftime(format_str)
    return time_string

import datetime

def millisecond_timestamp_to_string(timestamp):
    # 将时间戳转换为datetime对象
    dt_object = datetime.datetime.fromtimestamp(timestamp)
    # 将datetime对象转换为字符串
    dt_string = dt_object.strftime('%Y-%m-%d %H:%M:%S.%f')
    
    return dt_string


if __name__=="__main__":
    st=1686332793
    st=1686596928
    st=1628148392
    st=1625097600000
    # stt=timestamp_to_str(1687534426)
    stt=timestamp_to_str(st)
    print(stt)